home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / rpm / rpmpgp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-10-22  |  48.7 KB  |  1,438 lines

  1. #ifndef    H_RPMPGP
  2. #define    H_RPMPGP
  3.  
  4. /** \ingroup rpmio
  5.  * \file rpmio/rpmpgp.h
  6.  *
  7.  * OpenPGP constants and structures from RFC-2440.
  8.  *
  9.  * Text from RFC-2440 in comments is
  10.  *    Copyright (C) The Internet Society (1998).  All Rights Reserved.
  11.  */
  12.  
  13. #include <string.h>
  14.  
  15. #if !defined(_BEECRYPT_API_H)
  16. /*@-redef@*/
  17. typedef unsigned char byte;
  18. /*@=redef@*/
  19. #endif    /* _BEECRYPT_API_H */
  20.  
  21. /**
  22.  */
  23. typedef /*@abstract@*/ struct DIGEST_CTX_s * DIGEST_CTX;
  24.  
  25. /**
  26.  */
  27. typedef const struct pgpValTbl_s {
  28.     int val;
  29. /*@observer@*/ const char * str;
  30. } * pgpValTbl;
  31.  
  32. /**
  33.  * 4.3. Packet Tags
  34.  * 
  35.  * The packet tag denotes what type of packet the body holds. Note that
  36.  * old format headers can only have tags less than 16, whereas new
  37.  * format headers can have tags as great as 63.
  38.  */
  39. typedef enum pgpTag_e {
  40.     PGPTAG_RESERVED        =  0, /*!< Reserved/Invalid */
  41.     PGPTAG_PUBLIC_SESSION_KEY    =  1, /*!< Public-Key Encrypted Session Key */
  42.     PGPTAG_SIGNATURE        =  2, /*!< Signature */
  43.     PGPTAG_SYMMETRIC_SESSION_KEY=  3, /*!< Symmetric-Key Encrypted Session Key*/
  44.     PGPTAG_ONEPASS_SIGNATURE    =  4, /*!< One-Pass Signature */
  45.     PGPTAG_SECRET_KEY        =  5, /*!< Secret Key */
  46.     PGPTAG_PUBLIC_KEY        =  6, /*!< Public Key */
  47.     PGPTAG_SECRET_SUBKEY    =  7, /*!< Secret Subkey */
  48.     PGPTAG_COMPRESSED_DATA    =  8, /*!< Compressed Data */
  49.     PGPTAG_SYMMETRIC_DATA    =  9, /*!< Symmetrically Encrypted Data */
  50.     PGPTAG_MARKER        = 10, /*!< Marker */
  51.     PGPTAG_LITERAL_DATA        = 11, /*!< Literal Data */
  52.     PGPTAG_TRUST        = 12, /*!< Trust */
  53.     PGPTAG_USER_ID        = 13, /*!< User ID */
  54.     PGPTAG_PUBLIC_SUBKEY    = 14, /*!< Public Subkey */
  55.     PGPTAG_COMMENT_OLD        = 16, /*!< Comment (from OpenPGP draft) */
  56.     PGPTAG_PHOTOID        = 17, /*!< PGP's photo ID */
  57.     PGPTAG_ENCRYPTED_MDC    = 18, /*!< Integrity protected encrypted data */
  58.     PGPTAG_MDC            = 19, /*!< Manipulaion detection code packet */
  59.     PGPTAG_PRIVATE_60        = 60, /*!< Private or Experimental Values */
  60.     PGPTAG_COMMENT        = 61, /*!< Comment */
  61.     PGPTAG_PRIVATE_62        = 62, /*!< Private or Experimental Values */
  62.     PGPTAG_CONTROL        = 63  /*!< Control (GPG) */
  63. } pgpTag;
  64.  
  65. /**
  66.  */
  67. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  68. extern struct pgpValTbl_s pgpTagTbl[];
  69.  
  70. /**
  71.  * 5.1. Public-Key Encrypted Session Key Packets (Tag 1)
  72.  *
  73.  * A Public-Key Encrypted Session Key packet holds the session key used
  74.  * to encrypt a message. Zero or more Encrypted Session Key packets
  75.  * (either Public-Key or Symmetric-Key) may precede a Symmetrically
  76.  * Encrypted Data Packet, which holds an encrypted message.  The message
  77.  * is encrypted with the session key, and the session key is itself
  78.  * encrypted and stored in the Encrypted Session Key packet(s).  The
  79.  * Symmetrically Encrypted Data Packet is preceded by one Public-Key
  80.  * Encrypted Session Key packet for each OpenPGP key to which the
  81.  * message is encrypted.  The recipient of the message finds a session
  82.  * key that is encrypted to their public key, decrypts the session key,
  83.  * and then uses the session key to decrypt the message.
  84.  *
  85.  * The body of this packet consists of:
  86.  *   - A one-octet number giving the version number of the packet type.
  87.  *     The currently defined value for packet version is 3. An
  88.  *     implementation should accept, but not generate a version of 2,
  89.  *     which is equivalent to V3 in all other respects.
  90.  *   - An eight-octet number that gives the key ID of the public key
  91.  *     that the session key is encrypted to.
  92.  *   - A one-octet number giving the public key algorithm used.
  93.  *   - A string of octets that is the encrypted session key. This string
  94.  *     takes up the remainder of the packet, and its contents are
  95.  *     dependent on the public key algorithm used.
  96.  *
  97.  * Algorithm Specific Fields for RSA encryption
  98.  *   - multiprecision integer (MPI) of RSA encrypted value m**e mod n.
  99.  *
  100.  * Algorithm Specific Fields for Elgamal encryption:
  101.  *   - MPI of Elgamal (Diffie-Hellman) value g**k mod p.
  102.  *   - MPI of Elgamal (Diffie-Hellman) value m * y**k mod p.
  103.  */
  104. typedef struct pgpPktPubkey_s {
  105.     byte version;    /*!< version number (generate 3, accept 2). */
  106.     byte keyid[8];    /*!< key ID of the public key for session key. */
  107.     byte algo;        /*!< public key algorithm used. */
  108. } pgpPktPubkey;
  109.  
  110.  
  111. /**
  112.  * 5.2.1. Signature Types
  113.  * 
  114.  * There are a number of possible meanings for a signature, which are
  115.  * specified in a signature type octet in any given signature.
  116.  */
  117. /*@-typeuse@*/
  118. typedef enum pgpSigType_e {
  119.     PGPSIGTYPE_BINARY         = 0x00, /*!< Binary document */
  120.     PGPSIGTYPE_TEXT         = 0x01, /*!< Canonical text document */
  121.     PGPSIGTYPE_STANDALONE     = 0x02, /*!< Standalone */
  122.     PGPSIGTYPE_GENERIC_CERT     = 0x10,
  123.         /*!< Generic certification of a User ID & Public Key */
  124.     PGPSIGTYPE_PERSONA_CERT     = 0x11,
  125.         /*!< Persona certification of a User ID & Public Key */
  126.     PGPSIGTYPE_CASUAL_CERT     = 0x12,
  127.         /*!< Casual certification of a User ID & Public Key */
  128.     PGPSIGTYPE_POSITIVE_CERT     = 0x13,
  129.         /*!< Positive certification of a User ID & Public Key */
  130.     PGPSIGTYPE_SUBKEY_BINDING     = 0x18, /*!< Subkey Binding */
  131.     PGPSIGTYPE_SIGNED_KEY     = 0x1F, /*!< Signature directly on a key */
  132.     PGPSIGTYPE_KEY_REVOKE     = 0x20, /*!< Key revocation */
  133.     PGPSIGTYPE_SUBKEY_REVOKE     = 0x28, /*!< Subkey revocation */
  134.     PGPSIGTYPE_CERT_REVOKE     = 0x30, /*!< Certification revocation */
  135.     PGPSIGTYPE_TIMESTAMP     = 0x40  /*!< Timestamp */
  136. } pgpSigType;
  137. /*@=typeuse@*/
  138.  
  139. /**
  140.  */
  141. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  142. extern struct pgpValTbl_s pgpSigTypeTbl[];
  143.  
  144. /**
  145.  * 9.1. Public Key Algorithms
  146.  *
  147. \verbatim
  148.        ID           Algorithm
  149.        --           ---------
  150.        1          - RSA (Encrypt or Sign)
  151.        2          - RSA Encrypt-Only
  152.        3          - RSA Sign-Only
  153.        16         - Elgamal (Encrypt-Only), see [ELGAMAL]
  154.        17         - DSA (Digital Signature Standard)
  155.        18         - Reserved for Elliptic Curve
  156.        19         - Reserved for ECDSA
  157.        20         - Elgamal (Encrypt or Sign)
  158.        21         - Reserved for Diffie-Hellman (X9.42,
  159.                     as defined for IETF-S/MIME)
  160.        100 to 110 - Private/Experimental algorithm.
  161. \endverbatim
  162.  *
  163.  * Implementations MUST implement DSA for signatures, and Elgamal for
  164.  * encryption. Implementations SHOULD implement RSA keys.
  165.  * Implementations MAY implement any other algorithm.
  166.  */
  167. /*@-typeuse@*/
  168. typedef enum pgpPubkeyAlgo_e {
  169.     PGPPUBKEYALGO_RSA        =  1,    /*!< RSA */
  170.     PGPPUBKEYALGO_RSA_ENCRYPT    =  2,    /*!< RSA(Encrypt-Only) */
  171.     PGPPUBKEYALGO_RSA_SIGN    =  3,    /*!< RSA(Sign-Only) */
  172.     PGPPUBKEYALGO_ELGAMAL_ENCRYPT = 16,    /*!< Elgamal(Encrypt-Only) */
  173.     PGPPUBKEYALGO_DSA        = 17,    /*!< DSA */
  174.     PGPPUBKEYALGO_EC        = 18,    /*!< Elliptic Curve */
  175.     PGPPUBKEYALGO_ECDSA        = 19,    /*!< ECDSA */
  176.     PGPPUBKEYALGO_ELGAMAL    = 20,    /*!< Elgamal */
  177.     PGPPUBKEYALGO_DH        = 21    /*!< Diffie-Hellman (X9.42) */
  178. } pgpPubkeyAlgo;
  179. /*@=typeuse@*/
  180.  
  181. /**
  182.  */
  183. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  184. extern struct pgpValTbl_s pgpPubkeyTbl[];
  185.  
  186. /**
  187.  * 9.2. Symmetric Key Algorithms
  188.  *
  189. \verbatim
  190.        ID           Algorithm
  191.        --           ---------
  192.        0          - Plaintext or unencrypted data
  193.        1          - IDEA [IDEA]
  194.        2          - Triple-DES (DES-EDE, as per spec -
  195.                     168 bit key derived from 192)
  196.        3          - CAST5 (128 bit key, as per RFC 2144)
  197.        4          - Blowfish (128 bit key, 16 rounds) [BLOWFISH]
  198.        5          - SAFER-SK128 (13 rounds) [SAFER]
  199.        6          - Reserved for DES/SK
  200.        7          - Reserved for AES with 128-bit key
  201.        8          - Reserved for AES with 192-bit key
  202.        9          - Reserved for AES with 256-bit key
  203.        100 to 110 - Private/Experimental algorithm.
  204. \endverbatim
  205.  *
  206.  * Implementations MUST implement Triple-DES. Implementations SHOULD
  207.  * implement IDEA and CAST5. Implementations MAY implement any other
  208.  * algorithm.
  209.  */
  210. /*@-typeuse@*/
  211. typedef enum pgpSymkeyAlgo_e {
  212.     PGPSYMKEYALGO_PLAINTEXT    =  0,    /*!< Plaintext */
  213.     PGPSYMKEYALGO_IDEA        =  1,    /*!< IDEA */
  214.     PGPSYMKEYALGO_TRIPLE_DES    =  2,    /*!< 3DES */
  215.     PGPSYMKEYALGO_CAST5        =  3,    /*!< CAST5 */
  216.     PGPSYMKEYALGO_BLOWFISH    =  4,    /*!< BLOWFISH */
  217.     PGPSYMKEYALGO_SAFER        =  5,    /*!< SAFER */
  218.     PGPSYMKEYALGO_DES_SK    =  6,    /*!< DES/SK */
  219.     PGPSYMKEYALGO_AES_128    =  7,    /*!< AES(128-bit key) */
  220.     PGPSYMKEYALGO_AES_192    =  8,    /*!< AES(192-bit key) */
  221.     PGPSYMKEYALGO_AES_256    =  9,    /*!< AES(256-bit key) */
  222.     PGPSYMKEYALGO_TWOFISH    = 10,    /*!< TWOFISH(256-bit key) */
  223.     PGPSYMKEYALGO_NOENCRYPT    = 110    /*!< no encryption */
  224. } pgpSymkeyAlgo;
  225. /*@=typeuse@*/
  226.  
  227. /**
  228.  * Symmetric key (string, value) pairs.
  229.  */
  230. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  231. extern struct pgpValTbl_s pgpSymkeyTbl[];
  232.  
  233. /**
  234.  * 9.3. Compression Algorithms
  235.  *
  236. \verbatim
  237.        ID           Algorithm
  238.        --           ---------
  239.        0          - Uncompressed
  240.        1          - ZIP (RFC 1951)
  241.        2          - ZLIB (RFC 1950)
  242.        100 to 110 - Private/Experimental algorithm.
  243. \endverbatim
  244.  *
  245.  * Implementations MUST implement uncompressed data. Implementations
  246.  * SHOULD implement ZIP. Implementations MAY implement ZLIB.
  247.  */
  248. /*@-typeuse@*/
  249. typedef enum pgpCompressAlgo_e {
  250.     PGPCOMPRESSALGO_NONE    =  0,    /*!< Uncompressed */
  251.     PGPCOMPRESSALGO_ZIP        =  1,    /*!< ZIP */
  252.     PGPCOMPRESSALGO_ZLIB    =  2,    /*!< ZLIB */
  253.     PGPCOMPRESSALGO_BZIP2    =  3    /*!< BZIP2 */
  254. } pgpCompressAlgo;
  255. /*@=typeuse@*/
  256.  
  257. /**
  258.  * Compression (string, value) pairs.
  259.  */
  260. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  261. extern struct pgpValTbl_s pgpCompressionTbl[];
  262.  
  263. /**
  264.  * 9.4. Hash Algorithms
  265.  *
  266. \verbatim
  267.        ID           Algorithm                              Text Name
  268.        --           ---------                              ---- ----
  269.        1          - MD5                                    "MD5"
  270.        2          - SHA-1                                  "SHA1"
  271.        3          - RIPE-MD/160                            "RIPEMD160"
  272.        4          - Reserved for double-width SHA (experimental)
  273.        5          - MD2                                    "MD2"
  274.        6          - Reserved for TIGER/192                 "TIGER192"
  275.        7          - Reserved for HAVAL (5 pass, 160-bit)
  276.        "HAVAL-5-160"
  277.        100 to 110 - Private/Experimental algorithm.
  278. \endverbatim
  279.  *
  280.  * Implementations MUST implement SHA-1. Implementations SHOULD
  281.  * implement MD5.
  282.  * @todo Add SHA256.
  283.  */
  284. typedef enum pgpHashAlgo_e {
  285.     PGPHASHALGO_MD5        =  1,    /*!< MD5 */
  286.     PGPHASHALGO_SHA1        =  2,    /*!< SHA1 */
  287.     PGPHASHALGO_RIPEMD160    =  3,    /*!< RIPEMD160 */
  288.     PGPHASHALGO_MD2        =  5,    /*!< MD2 */
  289.     PGPHASHALGO_TIGER192    =  6,    /*!< TIGER192 */
  290.     PGPHASHALGO_HAVAL_5_160    =  7,    /*!< HAVAL-5-160 */
  291.     PGPHASHALGO_SHA256        =  8,    /*!< SHA256 */
  292.     PGPHASHALGO_SHA384        =  9,    /*!< SHA384 */
  293.     PGPHASHALGO_SHA512        = 10,    /*!< SHA512 */
  294. } pgpHashAlgo;
  295.  
  296. /**
  297.  * Hash (string, value) pairs.
  298.  */
  299. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  300. extern struct pgpValTbl_s pgpHashTbl[];
  301.  
  302. /**
  303.  * 5.2.2. Version 3 Signature Packet Format
  304.  * 
  305.  * The body of a version 3 Signature Packet contains:
  306.  *   - One-octet version number (3).
  307.  *   - One-octet length of following hashed material.  MUST be 5.
  308.  *       - One-octet signature type.
  309.  *       - Four-octet creation time.
  310.  *   - Eight-octet key ID of signer.
  311.  *   - One-octet public key algorithm.
  312.  *   - One-octet hash algorithm.
  313.  *   - Two-octet field holding left 16 bits of signed hash value.
  314.  *   - One or more multi-precision integers comprising the signature.
  315.  *
  316.  * Algorithm Specific Fields for RSA signatures:
  317.  *   - multiprecision integer (MPI) of RSA signature value m**d.
  318.  *
  319.  * Algorithm Specific Fields for DSA signatures:
  320.  *   - MPI of DSA value r.
  321.  *   - MPI of DSA value s.
  322.  */
  323. typedef struct pgpPktSigV3_s {
  324.     byte version;    /*!< version number (3). */
  325.     byte hashlen;    /*!< length of following hashed material. MUST be 5. */
  326.     byte sigtype;    /*!< signature type. */
  327.     byte time[4];    /*!< 4 byte creation time. */
  328.     byte signid[8];    /*!< key ID of signer. */
  329.     byte pubkey_algo;    /*!< public key algorithm. */
  330.     byte hash_algo;    /*!< hash algorithm. */
  331.     byte signhash16[2];    /*!< left 16 bits of signed hash value. */
  332. } * pgpPktSigV3;
  333.  
  334. /**
  335.  * 5.2.3. Version 4 Signature Packet Format
  336.  * 
  337.  * The body of a version 4 Signature Packet contains:
  338.  *   - One-octet version number (4).
  339.  *   - One-octet signature type.
  340.  *   - One-octet public key algorithm.
  341.  *   - One-octet hash algorithm.
  342.  *   - Two-octet scalar octet count for following hashed subpacket
  343.  *     data. Note that this is the length in octets of all of the hashed
  344.  *     subpackets; a pointer incremented by this number will skip over
  345.  *     the hashed subpackets.
  346.  *   - Hashed subpacket data. (zero or more subpackets)
  347.  *   - Two-octet scalar octet count for following unhashed subpacket
  348.  *     data. Note that this is the length in octets of all of the
  349.  *     unhashed subpackets; a pointer incremented by this number will
  350.  *     skip over the unhashed subpackets.
  351.  *   - Unhashed subpacket data. (zero or more subpackets)
  352.  *   - Two-octet field holding left 16 bits of signed hash value.
  353.  *   - One or more multi-precision integers comprising the signature.
  354.  */
  355. typedef struct pgpPktSigV4_s {
  356.     byte version;    /*!< version number (4). */
  357.     byte sigtype;    /*!< signature type. */
  358.     byte pubkey_algo;    /*!< public key algorithm. */
  359.     byte hash_algo;    /*!< hash algorithm. */
  360.     byte hashlen[2];    /*!< length of following hashed material. */
  361. } * pgpPktSigV4;
  362.  
  363. /**
  364.  * 5.2.3.1. Signature Subpacket Specification
  365.  * 
  366.  * The subpacket fields consist of zero or more signature subpackets.
  367.  * Each set of subpackets is preceded by a two-octet scalar count of the
  368.  * length of the set of subpackets.
  369.  *
  370.  * Each subpacket consists of a subpacket header and a body.  The header
  371.  * consists of:
  372.  *   - the subpacket length (1,  2, or 5 octets)
  373.  *   - the subpacket type (1 octet)
  374.  * and is followed by the subpacket specific data.
  375.  *
  376.  * The length includes the type octet but not this length. Its format is
  377.  * similar to the "new" format packet header lengths, but cannot have
  378.  * partial body lengths. That is:
  379. \verbatim
  380.        if the 1st octet <  192, then
  381.            lengthOfLength = 1
  382.            subpacketLen = 1st_octet
  383.  
  384.        if the 1st octet >= 192 and < 255, then
  385.            lengthOfLength = 2
  386.            subpacketLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192
  387.  
  388.        if the 1st octet = 255, then
  389.            lengthOfLength = 5
  390.            subpacket length = [four-octet scalar starting at 2nd_octet]
  391. \endverbatim
  392.  *
  393.  * The value of the subpacket type octet may be:
  394.  *
  395. \verbatim
  396.        2 = signature creation time
  397.        3 = signature expiration time
  398.        4 = exportable certification
  399.        5 = trust signature
  400.        6 = regular expression
  401.        7 = revocable
  402.        9 = key expiration time
  403.        10 = placeholder for backward compatibility
  404.        11 = preferred symmetric algorithms
  405.        12 = revocation key
  406.        16 = issuer key ID
  407.        20 = notation data
  408.        21 = preferred hash algorithms
  409.        22 = preferred compression algorithms
  410.        23 = key server preferences
  411.        24 = preferred key server
  412.        25 = primary user id
  413.        26 = policy URL
  414.        27 = key flags
  415.        28 = signer's user id
  416.        29 = reason for revocation
  417.        100 to 110 = internal or user-defined
  418. \endverbatim
  419.  *
  420.  * An implementation SHOULD ignore any subpacket of a type that it does
  421.  * not recognize.
  422.  *
  423.  * Bit 7 of the subpacket type is the "critical" bit.  If set, it
  424.  * denotes that the subpacket is one that is critical for the evaluator
  425.  * of the signature to recognize.  If a subpacket is encountered that is
  426.  * marked critical but is unknown to the evaluating software, the
  427.  * evaluator SHOULD consider the signature to be in error.
  428.  */
  429. /*@-typeuse@*/
  430. typedef enum pgpSubType_e {
  431.     PGPSUBTYPE_NONE        =   0, /*!< none */
  432.     PGPSUBTYPE_SIG_CREATE_TIME    =   2, /*!< signature creation time */
  433.     PGPSUBTYPE_SIG_EXPIRE_TIME    =   3, /*!< signature expiration time */
  434.     PGPSUBTYPE_EXPORTABLE_CERT    =   4, /*!< exportable certification */
  435.     PGPSUBTYPE_TRUST_SIG    =   5, /*!< trust signature */
  436.     PGPSUBTYPE_REGEX        =   6, /*!< regular expression */
  437.     PGPSUBTYPE_REVOCABLE    =   7, /*!< revocable */
  438.     PGPSUBTYPE_KEY_EXPIRE_TIME    =   9, /*!< key expiration time */
  439.     PGPSUBTYPE_ARR        =  10, /*!< additional recipient request */
  440.     PGPSUBTYPE_PREFER_SYMKEY    =  11, /*!< preferred symmetric algorithms */
  441.     PGPSUBTYPE_REVOKE_KEY    =  12, /*!< revocation key */
  442.     PGPSUBTYPE_ISSUER_KEYID    =  16, /*!< issuer key ID */
  443.     PGPSUBTYPE_NOTATION        =  20, /*!< notation data */
  444.     PGPSUBTYPE_PREFER_HASH    =  21, /*!< preferred hash algorithms */
  445.     PGPSUBTYPE_PREFER_COMPRESS    =  22, /*!< preferred compression algorithms */
  446.     PGPSUBTYPE_KEYSERVER_PREFERS=  23, /*!< key server preferences */
  447.     PGPSUBTYPE_PREFER_KEYSERVER    =  24, /*!< preferred key server */
  448.     PGPSUBTYPE_PRIMARY_USERID    =  25, /*!< primary user id */
  449.     PGPSUBTYPE_POLICY_URL    =  26, /*!< policy URL */
  450.     PGPSUBTYPE_KEY_FLAGS    =  27, /*!< key flags */
  451.     PGPSUBTYPE_SIGNER_USERID    =  28, /*!< signer's user id */
  452.     PGPSUBTYPE_REVOKE_REASON    =  29, /*!< reason for revocation */
  453.     PGPSUBTYPE_FEATURES        =  30, /*!< feature flags (gpg) */
  454.     PGPSUBTYPE_EMBEDDED_SIG    =  32, /*!< embedded signature (gpg) */
  455.  
  456.     PGPSUBTYPE_INTERNAL_100    = 100, /*!< internal or user-defined */
  457.     PGPSUBTYPE_INTERNAL_101    = 101, /*!< internal or user-defined */
  458.     PGPSUBTYPE_INTERNAL_102    = 102, /*!< internal or user-defined */
  459.     PGPSUBTYPE_INTERNAL_103    = 103, /*!< internal or user-defined */
  460.     PGPSUBTYPE_INTERNAL_104    = 104, /*!< internal or user-defined */
  461.     PGPSUBTYPE_INTERNAL_105    = 105, /*!< internal or user-defined */
  462.     PGPSUBTYPE_INTERNAL_106    = 106, /*!< internal or user-defined */
  463.     PGPSUBTYPE_INTERNAL_107    = 107, /*!< internal or user-defined */
  464.     PGPSUBTYPE_INTERNAL_108    = 108, /*!< internal or user-defined */
  465.     PGPSUBTYPE_INTERNAL_109    = 109, /*!< internal or user-defined */
  466.     PGPSUBTYPE_INTERNAL_110    = 110, /*!< internal or user-defined */
  467.  
  468.     PGPSUBTYPE_CRITICAL        = 128  /*!< critical subpacket marker */
  469. } pgpSubType;
  470. /*@=typeuse@*/
  471.  
  472. /**
  473.  * Subtype (string, value) pairs.
  474.  */
  475. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  476. extern struct pgpValTbl_s pgpSubTypeTbl[];
  477.  
  478. /**
  479.  * 5.2. Signature Packet (Tag 2)
  480.  *
  481.  * A signature packet describes a binding between some public key and
  482.  * some data. The most common signatures are a signature of a file or a
  483.  * block of text, and a signature that is a certification of a user ID.
  484.  *
  485.  * Two versions of signature packets are defined.  Version 3 provides
  486.  * basic signature information, while version 4 provides an expandable
  487.  * format with subpackets that can specify more information about the
  488.  * signature. PGP 2.6.x only accepts version 3 signatures.
  489.  *
  490.  * Implementations MUST accept V3 signatures. Implementations SHOULD
  491.  * generate V4 signatures.  Implementations MAY generate a V3 signature
  492.  * that can be verified by PGP 2.6.x.
  493.  *
  494.  * Note that if an implementation is creating an encrypted and signed
  495.  * message that is encrypted to a V3 key, it is reasonable to create a
  496.  * V3 signature.
  497.  */
  498. typedef union pgpPktSig_u {
  499.     struct pgpPktSigV3_s v3;
  500.     struct pgpPktSigV4_s v4;
  501. } * pgpPktSig;
  502.  
  503. /**
  504.  * 5.3. Symmetric-Key Encrypted Session-Key Packets (Tag 3)
  505.  *
  506.  * The Symmetric-Key Encrypted Session Key packet holds the symmetric-
  507.  * key encryption of a session key used to encrypt a message.  Zero or
  508.  * more Encrypted Session Key packets and/or Symmetric-Key Encrypted
  509.  * Session Key packets may precede a Symmetrically Encrypted Data Packet
  510.  * that holds an encrypted message.  The message is encrypted with a
  511.  * session key, and the session key is itself encrypted and stored in
  512.  * the Encrypted Session Key packet or the Symmetric-Key Encrypted
  513.  * Session Key packet.
  514.  *
  515.  * If the Symmetrically Encrypted Data Packet is preceded by one or more
  516.  * Symmetric-Key Encrypted Session Key packets, each specifies a
  517.  * passphrase that may be used to decrypt the message.  This allows a
  518.  * message to be encrypted to a number of public keys, and also to one
  519.  * or more pass phrases. This packet type is new, and is not generated
  520.  * by PGP 2.x or PGP 5.0.
  521.  *
  522.  * The body of this packet consists of:
  523.  *   - A one-octet version number. The only currently defined version
  524.  *     is 4.
  525.  *   - A one-octet number describing the symmetric algorithm used.
  526.  *   - A string-to-key (S2K) specifier, length as defined above.
  527.  *   - Optionally, the encrypted session key itself, which is decrypted
  528.  *     with the string-to-key object.
  529.  *
  530.  */
  531. typedef struct pgpPktSymkey_s {
  532.     byte version;    /*!< version number (4). */
  533.     byte symkey_algo;
  534.     byte s2k[1];
  535. } pgpPktSymkey;
  536.  
  537. /**
  538.  * 5.4. One-Pass Signature Packets (Tag 4)
  539.  *
  540.  * The One-Pass Signature packet precedes the signed data and contains
  541.  * enough information to allow the receiver to begin calculating any
  542.  * hashes needed to verify the signature.  It allows the Signature
  543.  * Packet to be placed at the end of the message, so that the signer can
  544.  * compute the entire signed message in one pass.
  545.  *
  546.  * A One-Pass Signature does not interoperate with PGP 2.6.x or earlier.
  547.  *
  548.  * The body of this packet consists of:
  549.  *   - A one-octet version number. The current version is 3.
  550.  *   - A one-octet signature type. Signature types are described in
  551.  *     section 5.2.1.
  552.  *   - A one-octet number describing the hash algorithm used.
  553.  *   - A one-octet number describing the public key algorithm used.
  554.  *   - An eight-octet number holding the key ID of the signing key.
  555.  *   - A one-octet number holding a flag showing whether the signature
  556.  *     is nested.  A zero value indicates that the next packet is
  557.  *     another One-Pass Signature packet that describes another
  558.  *     signature to be applied to the same message data.
  559.  *
  560.  * Note that if a message contains more than one one-pass signature,
  561.  * then the signature packets bracket the message; that is, the first
  562.  * signature packet after the message corresponds to the last one-pass
  563.  * packet and the final signature packet corresponds to the first one-
  564.  * pass packet.
  565.  */
  566. typedef struct pgpPktOnepass_s {
  567.     byte version;    /*!< version number (3). */
  568.     byte sigtype;    /*!< signature type. */
  569.     byte hash_algo;    /*!< hash algorithm. */
  570.     byte pubkey_algo;    /*!< public key algorithm. */
  571.     byte signid[8];    /*!< key ID of signer. */
  572.     byte nested;
  573. } * pgpPktOnepass;
  574.  
  575. /**
  576.  * 5.5.1. Key Packet Variants
  577.  *
  578.  * 5.5.1.1. Public Key Packet (Tag 6)
  579.  *
  580.  * A Public Key packet starts a series of packets that forms an OpenPGP
  581.  * key (sometimes called an OpenPGP certificate).
  582.  *
  583.  * 5.5.1.2. Public Subkey Packet (Tag 14)
  584.  *
  585.  * A Public Subkey packet (tag 14) has exactly the same format as a
  586.  * Public Key packet, but denotes a subkey. One or more subkeys may be
  587.  * associated with a top-level key.  By convention, the top-level key
  588.  * provides signature services, and the subkeys provide encryption
  589.  * services.
  590.  *
  591.  * Note: in PGP 2.6.x, tag 14 was intended to indicate a comment packet.
  592.  * This tag was selected for reuse because no previous version of PGP
  593.  * ever emitted comment packets but they did properly ignore them.
  594.  * Public Subkey packets are ignored by PGP 2.6.x and do not cause it to
  595.  * fail, providing a limited degree of backward compatibility.
  596.  *
  597.  * 5.5.1.3. Secret Key Packet (Tag 5)
  598.  *
  599.  * A Secret Key packet contains all the information that is found in a
  600.  * Public Key packet, including the public key material, but also
  601.  * includes the secret key material after all the public key fields.
  602.  *
  603.  * 5.5.1.4. Secret Subkey Packet (Tag 7)
  604.  *
  605.  * A Secret Subkey packet (tag 7) is the subkey analog of the Secret Key
  606.  * packet, and has exactly the same format.
  607.  *
  608.  * 5.5.2. Public Key Packet Formats
  609.  *
  610.  * There are two versions of key-material packets. Version 3 packets
  611.  * were first generated by PGP 2.6. Version 2 packets are identical in
  612.  * format to Version 3 packets, but are generated by PGP 2.5 or before.
  613.  * V2 packets are deprecated and they MUST NOT be generated.  PGP 5.0
  614.  * introduced version 4 packets, with new fields and semantics.  PGP
  615.  * 2.6.x will not accept key-material packets with versions greater than
  616.  * 3.
  617.  *
  618.  * OpenPGP implementations SHOULD create keys with version 4 format. An
  619.  * implementation MAY generate a V3 key to ensure interoperability with
  620.  * old software; note, however, that V4 keys correct some security
  621.  * deficiencies in V3 keys. These deficiencies are described below. An
  622.  * implementation MUST NOT create a V3 key with a public key algorithm
  623.  * other than RSA.
  624.  *
  625.  * A version 3 public key or public subkey packet contains:
  626.  *   - A one-octet version number (3).
  627.  *   - A four-octet number denoting the time that the key was created.
  628.  *   - A two-octet number denoting the time in days that this key is
  629.  *     valid. If this number is zero, then it does not expire.
  630.  *   - A one-octet number denoting the public key algorithm of this key
  631.  *   - A series of multi-precision integers comprising the key
  632.  *     material:
  633.  *       - a multiprecision integer (MPI) of RSA public modulus n;
  634.  *       - an MPI of RSA public encryption exponent e.
  635.  *
  636.  * V3 keys SHOULD only be used for backward compatibility because of
  637.  * three weaknesses in them. First, it is relatively easy to construct a
  638.  * V3 key that has the same key ID as any other key because the key ID
  639.  * is simply the low 64 bits of the public modulus. Secondly, because
  640.  * the fingerprint of a V3 key hashes the key material, but not its
  641.  * length, which increases the opportunity for fingerprint collisions.
  642.  * Third, there are minor weaknesses in the MD5 hash algorithm that make
  643.  * developers prefer other algorithms. See below for a fuller discussion
  644.  * of key IDs and fingerprints.
  645.  *
  646.  */
  647. typedef struct pgpPktKeyV3_s {
  648.     byte version;    /*!< version number (3). */
  649.     byte time[4];    /*!< time that the key was created. */
  650.     byte valid[2];    /*!< time in days that this key is valid. */
  651.     byte pubkey_algo;    /*!< public key algorithm. */
  652. } * pgpPktKeyV3;
  653.  
  654. /**
  655.  * The version 4 format is similar to the version 3 format except for
  656.  * the absence of a validity period.  This has been moved to the
  657.  * signature packet.  In addition, fingerprints of version 4 keys are
  658.  * calculated differently from version 3 keys, as described in section
  659.  * "Enhanced Key Formats."
  660.  *
  661.  * A version 4 packet contains:
  662.  *   - A one-octet version number (4).
  663.  *   - A four-octet number denoting the time that the key was created.
  664.  *   - A one-octet number denoting the public key algorithm of this key
  665.  *   - A series of multi-precision integers comprising the key
  666.  *     material.  This algorithm-specific portion is:
  667.  *
  668.  *     Algorithm Specific Fields for RSA public keys:
  669.  *       - multiprecision integer (MPI) of RSA public modulus n;
  670.  *       - MPI of RSA public encryption exponent e.
  671.  *
  672.  *     Algorithm Specific Fields for DSA public keys:
  673.  *       - MPI of DSA prime p;
  674.  *       - MPI of DSA group order q (q is a prime divisor of p-1);
  675.  *       - MPI of DSA group generator g;
  676.  *       - MPI of DSA public key value y (= g**x where x is secret).
  677.  *
  678.  *     Algorithm Specific Fields for Elgamal public keys:
  679.  *       - MPI of Elgamal prime p;
  680.  *       - MPI of Elgamal group generator g;
  681.  *       - MPI of Elgamal public key value y (= g**x where x is
  682.  *         secret).
  683.  *
  684.  */
  685. typedef struct pgpPktKeyV4_s {
  686.     byte version;    /*!< version number (4). */
  687.     byte time[4];    /*!< time that the key was created. */
  688.     byte pubkey_algo;    /*!< public key algorithm. */
  689. } * pgpPktKeyV4;
  690.  
  691. /**
  692.  * 5.5.3. Secret Key Packet Formats
  693.  *
  694.  * The Secret Key and Secret Subkey packets contain all the data of the
  695.  * Public Key and Public Subkey packets, with additional algorithm-
  696.  * specific secret key data appended, in encrypted form.
  697.  *
  698.  * The packet contains:
  699.  *   - A Public Key or Public Subkey packet, as described above
  700.  *   - One octet indicating string-to-key usage conventions.  0
  701.  *     indicates that the secret key data is not encrypted.  255
  702.  *     indicates that a string-to-key specifier is being given.  Any
  703.  *     other value is a symmetric-key encryption algorithm specifier.
  704.  *   - [Optional] If string-to-key usage octet was 255, a one-octet
  705.  *     symmetric encryption algorithm.
  706.  *   - [Optional] If string-to-key usage octet was 255, a string-to-key
  707.  *     specifier.  The length of the string-to-key specifier is implied
  708.  *     by its type, as described above.
  709.  *   - [Optional] If secret data is encrypted, eight-octet Initial
  710.  *     Vector (IV).
  711.  *   - Encrypted multi-precision integers comprising the secret key
  712.  *     data. These algorithm-specific fields are as described below.
  713.  *   - Two-octet checksum of the plaintext of the algorithm-specific
  714.  *     portion (sum of all octets, mod 65536).
  715.  *
  716.  *     Algorithm Specific Fields for RSA secret keys:
  717.  *     - multiprecision integer (MPI) of RSA secret exponent d.
  718.  *     - MPI of RSA secret prime value p.
  719.  *     - MPI of RSA secret prime value q (p < q).
  720.  *     - MPI of u, the multiplicative inverse of p, mod q.
  721.  *
  722.  *     Algorithm Specific Fields for DSA secret keys:
  723.  *     - MPI of DSA secret exponent x.
  724.  *
  725.  *     Algorithm Specific Fields for Elgamal secret keys:
  726.  *     - MPI of Elgamal secret exponent x.
  727.  *
  728.  * Secret MPI values can be encrypted using a passphrase.  If a string-
  729.  * to-key specifier is given, that describes the algorithm for
  730.  * converting the passphrase to a key, else a simple MD5 hash of the
  731.  * passphrase is used.  Implementations SHOULD use a string-to-key
  732.  * specifier; the simple hash is for backward compatibility. The cipher
  733.  * for encrypting the MPIs is specified in the secret key packet.
  734.  *
  735.  * Encryption/decryption of the secret data is done in CFB mode using
  736.  * the key created from the passphrase and the Initial Vector from the
  737.  * packet. A different mode is used with V3 keys (which are only RSA)
  738.  * than with other key formats. With V3 keys, the MPI bit count prefix
  739.  * (i.e., the first two octets) is not encrypted.  Only the MPI non-
  740.  * prefix data is encrypted.  Furthermore, the CFB state is
  741.  * resynchronized at the beginning of each new MPI value, so that the
  742.  * CFB block boundary is aligned with the start of the MPI data.
  743.  *
  744.  * With V4 keys, a simpler method is used.  All secret MPI values are
  745.  * encrypted in CFB mode, including the MPI bitcount prefix.
  746.  *
  747.  * The 16-bit checksum that follows the algorithm-specific portion is
  748.  * the algebraic sum, mod 65536, of the plaintext of all the algorithm-
  749.  * specific octets (including MPI prefix and data).  With V3 keys, the
  750.  * checksum is stored in the clear.  With V4 keys, the checksum is
  751.  * encrypted like the algorithm-specific data.  This value is used to
  752.  * check that the passphrase was correct.
  753.  *
  754.  */
  755. typedef union pgpPktKey_u {
  756.     struct pgpPktKeyV3_s v3;
  757.     struct pgpPktKeyV4_s v4;
  758. } pgpPktKey;
  759.  
  760. /*
  761.  * 5.6. Compressed Data Packet (Tag 8)
  762.  *
  763.  * The Compressed Data packet contains compressed data. Typically, this
  764.  * packet is found as the contents of an encrypted packet, or following
  765.  * a Signature or One-Pass Signature packet, and contains literal data
  766.  * packets.
  767.  *
  768.  * The body of this packet consists of:
  769.  *   - One octet that gives the algorithm used to compress the packet.
  770.  *   - The remainder of the packet is compressed data.
  771.  *
  772.  * A Compressed Data Packet's body contains an block that compresses
  773.  * some set of packets. See section "Packet Composition" for details on
  774.  * how messages are formed.
  775.  *
  776.  * ZIP-compressed packets are compressed with raw RFC 1951 DEFLATE
  777.  * blocks. Note that PGP V2.6 uses 13 bits of compression. If an
  778.  * implementation uses more bits of compression, PGP V2.6 cannot
  779.  * decompress it.
  780.  *
  781.  * ZLIB-compressed packets are compressed with RFC 1950 ZLIB-style
  782.  * blocks.
  783.  */
  784. typedef struct pgpPktCdata_s {
  785.     byte compressalgo;
  786.     byte data[1];
  787. } pgpPktCdata;
  788.  
  789. /*
  790.  * 5.7. Symmetrically Encrypted Data Packet (Tag 9)
  791.  *
  792.  * The Symmetrically Encrypted Data packet contains data encrypted with
  793.  * a symmetric-key algorithm. When it has been decrypted, it will
  794.  * typically contain other packets (often literal data packets or
  795.  * compressed data packets).
  796.  *
  797.  * The body of this packet consists of:
  798.  *   - Encrypted data, the output of the selected symmetric-key cipher
  799.  *     operating in PGP's variant of Cipher Feedback (CFB) mode.
  800.  *
  801.  * The symmetric cipher used may be specified in an Public-Key or
  802.  * Symmetric-Key Encrypted Session Key packet that precedes the
  803.  * Symmetrically Encrypted Data Packet.  In that case, the cipher
  804.  * algorithm octet is prefixed to the session key before it is
  805.  * encrypted.  If no packets of these types precede the encrypted data,
  806.  * the IDEA algorithm is used with the session key calculated as the MD5
  807.  * hash of the passphrase.
  808.  *
  809.  * The data is encrypted in CFB mode, with a CFB shift size equal to the
  810.  * cipher's block size.  The Initial Vector (IV) is specified as all
  811.  * zeros.  Instead of using an IV, OpenPGP prefixes a 10-octet string to
  812.  * the data before it is encrypted.  The first eight octets are random,
  813.  * and the 9th and 10th octets are copies of the 7th and 8th octets,
  814.  * respectively. After encrypting the first 10 octets, the CFB state is
  815.  * resynchronized if the cipher block size is 8 octets or less.  The
  816.  * last 8 octets of ciphertext are passed through the cipher and the
  817.  * block boundary is reset.
  818.  *
  819.  * The repetition of 16 bits in the 80 bits of random data prefixed to
  820.  * the message allows the receiver to immediately check whether the
  821.  * session key is incorrect.
  822.  */
  823. typedef struct pgpPktEdata_s {
  824.     byte data[1];
  825. } pgpPktEdata;
  826.  
  827. /*
  828.  * 5.8. Marker Packet (Obsolete Literal Packet) (Tag 10)
  829.  *
  830.  * An experimental version of PGP used this packet as the Literal
  831.  * packet, but no released version of PGP generated Literal packets with
  832.  * this tag. With PGP 5.x, this packet has been re-assigned and is
  833.  * reserved for use as the Marker packet.
  834.  *
  835.  * The body of this packet consists of:
  836.  *   - The three octets 0x50, 0x47, 0x50 (which spell "PGP" in UTF-8).
  837.  *
  838.  * Such a packet MUST be ignored when received.  It may be placed at the
  839.  * beginning of a message that uses features not available in PGP 2.6.x
  840.  * in order to cause that version to report that newer software is
  841.  * necessary to process the message.
  842.  */
  843. /*
  844.  * 5.9. Literal Data Packet (Tag 11)
  845.  *
  846.  * A Literal Data packet contains the body of a message; data that is
  847.  * not to be further interpreted.
  848.  *
  849.  * The body of this packet consists of:
  850.  *   - A one-octet field that describes how the data is formatted.
  851.  *
  852.  * If it is a 'b' (0x62), then the literal packet contains binary data.
  853.  * If it is a 't' (0x74), then it contains text data, and thus may need
  854.  * line ends converted to local form, or other text-mode changes.  RFC
  855.  * 1991 also defined a value of 'l' as a 'local' mode for machine-local
  856.  * conversions.  This use is now deprecated.
  857.  *   - File name as a string (one-octet length, followed by file name),
  858.  *     if the encrypted data should be saved as a file.
  859.  *
  860.  * If the special name "_CONSOLE" is used, the message is considered to
  861.  * be "for your eyes only".  This advises that the message data is
  862.  * unusually sensitive, and the receiving program should process it more
  863.  * carefully, perhaps avoiding storing the received data to disk, for
  864.  * example.
  865.  *   - A four-octet number that indicates the modification date of the
  866.  *     file, or the creation time of the packet, or a zero that
  867.  *     indicates the present time.
  868.  *   - The remainder of the packet is literal data.
  869.  *
  870.  * Text data is stored with <CR><LF> text endings (i.e. network-normal
  871.  * line endings).  These should be converted to native line endings by
  872.  * the receiving software.
  873.  */
  874. typedef struct pgpPktLdata_s {
  875.     byte format;
  876.     byte filenamelen;
  877.     byte filename[1];
  878. } pgpPktLdata;
  879.  
  880. /*
  881.  * 5.10. Trust Packet (Tag 12)
  882.  *
  883.  * The Trust packet is used only within keyrings and is not normally
  884.  * exported.  Trust packets contain data that record the user's
  885.  * specifications of which key holders are trustworthy introducers,
  886.  * along with other information that implementing software uses for
  887.  * trust information.
  888.  *
  889.  * Trust packets SHOULD NOT be emitted to output streams that are
  890.  * transferred to other users, and they SHOULD be ignored on any input
  891.  * other than local keyring files.
  892.  */
  893. typedef struct pgpPktTrust_s {
  894.     byte flag;
  895. } pgpPktTrust;
  896.  
  897. /*
  898.  * 5.11. User ID Packet (Tag 13)
  899.  *
  900.  * A User ID packet consists of data that is intended to represent the
  901.  * name and email address of the key holder.  By convention, it includes
  902.  * an RFC 822 mail name, but there are no restrictions on its content.
  903.  * The packet length in the header specifies the length of the user id.
  904.  * If it is text, it is encoded in UTF-8.
  905.  *
  906.  */
  907. typedef struct pgpPktUid_s {
  908.     byte userid[1];
  909. } pgpPktUid;
  910.  
  911. /**
  912.  */
  913. union pgpPktPre_u {
  914.     pgpPktPubkey pubkey;    /*!< 5.1. Public-Key Encrypted Session Key */
  915.     pgpPktSig sig;        /*!< 5.2. Signature */
  916.     pgpPktSymkey symkey;    /*!< 5.3. Symmetric-Key Encrypted Session-Key */
  917.     pgpPktOnepass onepass;    /*!< 5.4. One-Pass Signature */
  918.     pgpPktKey key;        /*!< 5.5. Key Material */
  919.     pgpPktCdata cdata;        /*!< 5.6. Compressed Data */
  920.     pgpPktEdata edata;        /*!< 5.7. Symmetrically Encrypted Data */
  921.                 /*!< 5.8. Marker (obsolete) */
  922.     pgpPktLdata ldata;        /*!< 5.9. Literal Data */
  923.     pgpPktTrust tdata;        /*!< 5.10. Trust */
  924.     pgpPktUid uid;        /*!< 5.11. User ID */
  925. };
  926.  
  927. /**
  928.  */
  929. /*@-typeuse@*/
  930. typedef enum pgpArmor_e {
  931.     PGPARMOR_ERR_CRC_CHECK        = -7,
  932.     PGPARMOR_ERR_BODY_DECODE        = -6,
  933.     PGPARMOR_ERR_CRC_DECODE        = -5,
  934.     PGPARMOR_ERR_NO_END_PGP        = -4,
  935.     PGPARMOR_ERR_UNKNOWN_PREAMBLE_TAG    = -3,
  936.     PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE    = -2,
  937.     PGPARMOR_ERR_NO_BEGIN_PGP        = -1,
  938. #define    PGPARMOR_ERROR    PGPARMOR_ERR_NO_BEGIN_PGP
  939.     PGPARMOR_NONE        =  0,
  940.     PGPARMOR_MESSAGE        =  1, /*!< MESSAGE */
  941.     PGPARMOR_PUBKEY        =  2, /*!< PUBLIC KEY BLOCK */
  942.     PGPARMOR_SIGNATURE        =  3, /*!< SIGNATURE */
  943.     PGPARMOR_SIGNED_MESSAGE    =  4, /*!< SIGNED MESSAGE */
  944.     PGPARMOR_FILE        =  5, /*!< ARMORED FILE */
  945.     PGPARMOR_PRIVKEY        =  6, /*!< PRIVATE KEY BLOCK */
  946.     PGPARMOR_SECKEY        =  7  /*!< SECRET KEY BLOCK */
  947. } pgpArmor;
  948. /*@=typeuse@*/
  949.  
  950. /**
  951.  * Armor (string, value) pairs.
  952.  */
  953. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  954. extern struct pgpValTbl_s pgpArmorTbl[];
  955.  
  956. /**
  957.  */
  958. /*@-typeuse@*/
  959. typedef enum pgpArmorKey_e {
  960.     PGPARMORKEY_VERSION        = 1, /*!< Version: */
  961.     PGPARMORKEY_COMMENT        = 2, /*!< Comment: */
  962.     PGPARMORKEY_MESSAGEID    = 3, /*!< MessageID: */
  963.     PGPARMORKEY_HASH        = 4, /*!< Hash: */
  964.     PGPARMORKEY_CHARSET        = 5  /*!< Charset: */
  965. } pgpArmorKey;
  966. /*@=typeuse@*/
  967.  
  968. /**
  969.  * Armor key (string, value) pairs.
  970.  */
  971. /*@observer@*/ /*@unchecked@*/ /*@unused@*/
  972. extern struct pgpValTbl_s pgpArmorKeyTbl[];
  973.  
  974. /** \ingroup rpmio
  975.  * Bit(s) to control digest operation.
  976.  */
  977. typedef enum rpmDigestFlags_e {
  978.     RPMDIGEST_NONE    = 0
  979. } rpmDigestFlags;
  980.  
  981.  
  982. /*@-fcnuse@*/
  983. #ifdef __cplusplus
  984. extern "C" {
  985. #endif
  986.  
  987. /**
  988.  * Return (native-endian) integer from big-endian representation.
  989.  * @param s        pointer to big-endian integer
  990.  * @param nbytes    no. of bytes
  991.  * @return        native-endian integer
  992.  */
  993. /*@unused@*/ static inline
  994. unsigned int pgpGrab(const byte *s, int nbytes)
  995.     /*@*/
  996. {
  997.     unsigned int i = 0;
  998.     int nb = (nbytes <= sizeof(i) ? nbytes : sizeof(i));
  999. /*@-boundsread@*/
  1000.     while (nb--)
  1001.     i = (i << 8) | *s++;
  1002. /*@=boundsread@*/
  1003.     return i;
  1004. }
  1005.  
  1006. /**
  1007.  * Return length of an OpenPGP packet.
  1008.  * @param s        pointer to packet
  1009.  * @retval *lenp    no. of bytes in packet
  1010.  * @return        no. of bytes in length prefix
  1011.  */
  1012. /*@unused@*/ static inline
  1013. int pgpLen(const byte *s, /*@out@*/ unsigned int *lenp)
  1014.     /*@modifies *lenp @*/
  1015. {
  1016. /*@-boundswrite@*/
  1017.     if (*s < 192) {
  1018.     (*lenp) = *s++;
  1019.     return 1;
  1020.     } else if (*s < 255) {
  1021.     (*lenp) = ((((unsigned)s[0]) - 192) << 8) + s[1] + 192;
  1022.     return 2;
  1023.     } else {
  1024.     (*lenp) = pgpGrab(s+1, 4);
  1025.     return 5;
  1026.     }
  1027. /*@=boundswrite@*/
  1028. }
  1029.  
  1030. /**
  1031.  * Return no. of bits in a multiprecision integer.
  1032.  * @param p        pointer to multiprecision integer
  1033.  * @return        no. of bits
  1034.  */
  1035. /*@unused@*/ static inline
  1036. unsigned int pgpMpiBits(const byte *p)
  1037.     /*@requires maxRead(p) >= 1 @*/
  1038.     /*@*/
  1039. {
  1040.     return ((p[0] << 8) | p[1]);
  1041. }
  1042.  
  1043. /**
  1044.  * Return no. of bytes in a multiprecision integer.
  1045.  * @param p        pointer to multiprecision integer
  1046.  * @return        no. of bytes
  1047.  */
  1048. /*@unused@*/ static inline
  1049. unsigned int pgpMpiLen(const byte *p)
  1050.     /*@requires maxRead(p) >= 1 @*/
  1051.     /*@*/
  1052. {
  1053.     return (2 + ((pgpMpiBits(p)+7)>>3));
  1054. }
  1055.     
  1056. /**
  1057.  * Convert to hex.
  1058.  * @param t        target buffer (returned)
  1059.  * @param s        source bytes
  1060.  * @param nbytes    no. of bytes
  1061.  * @return        target buffer
  1062.  */
  1063. /*@unused@*/ static inline
  1064. char * pgpHexCvt(/*@returned@*/ char *t, const byte *s, int nbytes)
  1065.     /*@modifies *t @*/
  1066. {
  1067.     static char hex[] = "0123456789abcdef";
  1068. /*@-boundswrite@*/
  1069.     while (nbytes-- > 0) {
  1070.     unsigned int i;
  1071.     i = *s++;
  1072.     *t++ = hex[ (i >> 4) & 0xf ];
  1073.     *t++ = hex[ (i     ) & 0xf ];
  1074.     }
  1075.     *t = '\0';
  1076. /*@=boundswrite@*/
  1077.     return t;
  1078. }
  1079.  
  1080. /**
  1081.  * Return hex formatted representation of bytes.
  1082.  * @todo Remove static buffer. 
  1083.  * @param p        bytes
  1084.  * @param plen        no. of bytes
  1085.  * @return        hex formatted string
  1086.  */
  1087. /*@unused@*/ static inline /*@observer@*/
  1088. char * pgpHexStr(const byte *p, unsigned int plen)
  1089.     /*@*/
  1090. {
  1091.     static char prbuf[8*BUFSIZ];    /* XXX ick */
  1092.     char *t = prbuf;
  1093.     t = pgpHexCvt(t, p, plen);
  1094.     return prbuf;
  1095. }
  1096.  
  1097. /**
  1098.  * Return hex formatted representation of a multiprecision integer.
  1099.  * @todo Remove static buffer. 
  1100.  * @param p        bytes
  1101.  * @return        hex formatted string
  1102.  */
  1103. /*@unused@*/ static inline /*@observer@*/
  1104. const char * pgpMpiStr(const byte *p)
  1105.     /*@requires maxRead(p) >= 3 @*/
  1106.     /*@*/
  1107. {
  1108.     static char prbuf[8*BUFSIZ];    /* XXX ick */
  1109.     char *t = prbuf;
  1110.     sprintf(t, "[%4u]: ", pgpGrab(p, 2));
  1111.     t += strlen(t);
  1112.     t = pgpHexCvt(t, p+2, pgpMpiLen(p)-2);
  1113.     return prbuf;
  1114. }
  1115.  
  1116. /**
  1117.  * Return string representation of am OpenPGP value.
  1118.  * @param vs        table of (string,value) pairs
  1119.  * @param val        byte value to lookup
  1120.  * @return        string value of byte
  1121.  */
  1122. /*@unused@*/ static inline /*@observer@*/
  1123. const char * pgpValStr(pgpValTbl vs, byte val)
  1124.     /*@*/
  1125. {
  1126.     do {
  1127.     if (vs->val == val)
  1128.         break;
  1129.     } while ((++vs)->val != -1);
  1130.     return vs->str;
  1131. }
  1132.  
  1133. /**
  1134.  * Return value of an OpenPGP string.
  1135.  * @param vs        table of (string,value) pairs
  1136.  * @param s        string token to lookup
  1137.  * @param se        end-of-string address
  1138.  * @return        byte value
  1139.  */
  1140. /*@unused@*/ static inline
  1141. int pgpValTok(pgpValTbl vs, const char * s, const char * se)
  1142.     /*@*/
  1143. {
  1144.     do {
  1145.     int vlen = strlen(vs->str);
  1146.     if (vlen <= (se-s) && !strncmp(s, vs->str, vlen))
  1147.         break;
  1148.     } while ((++vs)->val != -1);
  1149.     return vs->val;
  1150. }
  1151.  
  1152. /*@-exportlocal@*/
  1153. /**
  1154.  * Print an OpenPGP value.
  1155.  * @param pre        output prefix
  1156.  * @param vs        table of (string,value) pairs
  1157.  * @param val        byte value to print
  1158.  */
  1159. void pgpPrtVal(const char * pre, pgpValTbl vs, byte val)
  1160.     /*@globals fileSystem @*/
  1161.     /*@modifies fileSystem @*/;
  1162.  
  1163. /**
  1164.  * Print/parse an OpenPGP subtype packet.
  1165.  * @param h        packet
  1166.  * @param hlen        packet length (no. of bytes)
  1167.  * @param sigtype    signature type
  1168.  * @return        0 on success
  1169.  */
  1170. int pgpPrtSubType(const byte *h, unsigned int hlen, pgpSigType sigtype)
  1171.     /*@globals fileSystem @*/
  1172.     /*@modifies fileSystem @*/;
  1173.  
  1174. /**
  1175.  * Print/parse an OpenPGP signature packet.
  1176.  * @param tag        packet tag
  1177.  * @param h        packet contents
  1178.  * @param hlen        packet length (no. of bytes)
  1179.  * @return        0 on success
  1180.  */
  1181. int pgpPrtSig(pgpTag tag, const byte *h, unsigned int hlen)
  1182.     /*@globals fileSystem, internalState @*/
  1183.     /*@modifies fileSystem, internalState @*/;
  1184.  
  1185. /**
  1186.  * Print/parse an OpenPGP key packet.
  1187.  * @param tag        packet tag
  1188.  * @param h        packet contents
  1189.  * @param hlen        packet length (no. of bytes)
  1190.  * @return        0 on success
  1191.  */
  1192. int pgpPrtKey(pgpTag tag, const byte *h, unsigned int hlen)
  1193.     /*@globals fileSystem, internalState @*/
  1194.     /*@modifies fileSystem, internalState @*/;
  1195.  
  1196. /**
  1197.  * Print/parse an OpenPGP userid packet.
  1198.  * @param tag        packet tag
  1199.  * @param h        packet contents
  1200.  * @param hlen        packet length (no. of bytes)
  1201.  * @return        0 on success
  1202.  */
  1203. int pgpPrtUserID(pgpTag tag, const byte *h, unsigned int hlen)
  1204.     /*@globals fileSystem, internalState @*/
  1205.     /*@modifies fileSystem, internalState @*/;
  1206.  
  1207. /**
  1208.  * Print/parse an OpenPGP comment packet.
  1209.  * @param tag        packet tag
  1210.  * @param h        packet contents
  1211.  * @param hlen        packet length (no. of bytes)
  1212.  * @return        0 on success
  1213.  */
  1214. int pgpPrtComment(pgpTag tag, const byte *h, unsigned int hlen)
  1215.     /*@globals fileSystem @*/
  1216.     /*@modifies fileSystem @*/;
  1217.  
  1218. /**
  1219.  * Calculate OpenPGP public key fingerprint.
  1220.  * @todo V3 non-RSA public keys not implemented.
  1221.  * @param pkt        OpenPGP packet (i.e. PGPTAG_PUBLIC_KEY)
  1222.  * @param pktlen    OpenPGP packet length (no. of bytes)
  1223.  * @retval keyid    publick key fingerprint
  1224.  * @return        0 on sucess, else -1
  1225.  */
  1226. int pgpPubkeyFingerprint(const byte * pkt, unsigned int pktlen,
  1227.         /*@out@*/ byte * keyid)
  1228.     /*@modifies *keyid @*/;
  1229.  
  1230. /**
  1231.  * Print/parse next OpenPGP packet.
  1232.  * @param pkt        OpenPGP packet
  1233.  * @param pleft        no. bytes remaining
  1234.  * @return        -1 on error, otherwise this packet length
  1235.  */
  1236. int pgpPrtPkt(const byte *pkt, unsigned int pleft)
  1237.     /*@globals fileSystem, internalState @*/
  1238.     /*@modifies fileSystem, internalState @*/;
  1239. /*@=exportlocal@*/
  1240.  
  1241. /**
  1242.  * Print/parse a OpenPGP packet(s).
  1243.  * @param pkts        OpenPGP packet(s)
  1244.  * @param pktlen    OpenPGP packet(s) length (no. of bytes)
  1245.  * @retval dig        parsed output of signature/pubkey packet parameters
  1246.  * @param printing    should packets be printed?
  1247.  * @return        -1 on error, 0 on success
  1248.  */
  1249. int pgpPrtPkts(const byte *pkts, unsigned int pktlen, pgpDig dig, int printing)
  1250.     /*@globals fileSystem, internalState @*/
  1251.     /*@modifies dig, fileSystem, internalState @*/;
  1252.  
  1253. /**
  1254.  * Parse armored OpenPGP packets from a file.
  1255.  * @param fn        file name
  1256.  * @retval pkt        dearmored OpenPGP packet(s)
  1257.  * @retval pktlen    dearmored OpenPGP packet(s) length in bytes
  1258.  * @return        type of armor found
  1259.  */
  1260. pgpArmor pgpReadPkts(const char * fn,
  1261.         /*@out@*/ const byte ** pkt, /*@out@*/ size_t * pktlen)
  1262.     /*@globals h_errno, fileSystem, internalState @*/
  1263.     /*@modifies *pkt, *pktlen, fileSystem, internalState @*/;
  1264.  
  1265. /**
  1266.  * Wrap a OpenPGP packets in ascii armor for transport.
  1267.  * @param atype        type of armor
  1268.  * @param s        binary pkt data
  1269.  * @param ns        binary pkt data length
  1270.  * @return        formatted string
  1271.  */
  1272. char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
  1273.     /*@*/;
  1274.  
  1275. /**
  1276.  * Create a container for parsed OpenPGP packates.
  1277.  * @return        container
  1278.  */
  1279. /*@only@*/
  1280. pgpDig pgpNewDig(void)
  1281.     /*@*/;
  1282.  
  1283. /**
  1284.  * Release (malloc'd) data from container.
  1285.  * @param dig        container
  1286.  */
  1287. void pgpCleanDig(/*@null@*/ pgpDig dig)
  1288.     /*@modifies dig @*/;
  1289.  
  1290. /**
  1291.  * Destroy a container for parsed OpenPGP packates.
  1292.  * @param dig        container
  1293.  * @return        NULL always
  1294.  */
  1295. /*@only@*/ /*@null@*/
  1296. pgpDig pgpFreeDig(/*@only@*/ /*@null@*/ pgpDig dig)
  1297.     /*@modifies dig @*/;
  1298.  
  1299. /**
  1300.  * Is buffer at beginning of an OpenPGP packet?
  1301.  * @param p        buffer
  1302.  * @return        1 if an OpenPGP packet, 0 otherwise
  1303.  */
  1304. /*@unused@*/ static inline
  1305. int pgpIsPkt(const byte * p)
  1306.     /*@*/
  1307. {
  1308. /*@-boundsread@*/
  1309.     unsigned int val = *p++;
  1310. /*@=boundsread@*/
  1311.     pgpTag tag;
  1312.     int rc;
  1313.  
  1314.     /* XXX can't deal with these. */
  1315.     if (!(val & 0x80))
  1316.     return 0;
  1317.  
  1318.     if (val & 0x40)
  1319.     tag = (pgpTag)(val & 0x3f);
  1320.     else
  1321.     tag = (pgpTag)((val >> 2) & 0xf);
  1322.  
  1323.     switch (tag) {
  1324.     case PGPTAG_MARKER:
  1325.     case PGPTAG_SYMMETRIC_SESSION_KEY:
  1326.     case PGPTAG_ONEPASS_SIGNATURE:
  1327.     case PGPTAG_PUBLIC_KEY:
  1328.     case PGPTAG_SECRET_KEY:
  1329.     case PGPTAG_PUBLIC_SESSION_KEY:
  1330.     case PGPTAG_SIGNATURE:
  1331.     case PGPTAG_COMMENT:
  1332.     case PGPTAG_COMMENT_OLD:
  1333.     case PGPTAG_LITERAL_DATA:
  1334.     case PGPTAG_COMPRESSED_DATA:
  1335.     case PGPTAG_SYMMETRIC_DATA:
  1336.     rc = 1;
  1337.     break;
  1338.     case PGPTAG_PUBLIC_SUBKEY:
  1339.     case PGPTAG_SECRET_SUBKEY:
  1340.     case PGPTAG_USER_ID:
  1341.     case PGPTAG_RESERVED:
  1342.     case PGPTAG_TRUST:
  1343.     case PGPTAG_PHOTOID:
  1344.     case PGPTAG_ENCRYPTED_MDC:
  1345.     case PGPTAG_MDC:
  1346.     case PGPTAG_PRIVATE_60:
  1347.     case PGPTAG_PRIVATE_62:
  1348.     case PGPTAG_CONTROL:
  1349.     default:
  1350.     rc = 0;
  1351.     break;
  1352.     }
  1353.  
  1354.     return rc;
  1355. }
  1356.  
  1357. #define CRC24_INIT    0xb704ce
  1358. #define CRC24_POLY    0x1864cfb
  1359.  
  1360. /**
  1361.  * Return CRC of a buffer.
  1362.  * @param octets    bytes
  1363.  * @param len        no. of bytes
  1364.  * @return        crc of buffer
  1365.  */
  1366. /*@unused@*/ static inline
  1367. unsigned int pgpCRC(const byte *octets, size_t len)
  1368.     /*@*/
  1369. {
  1370.     unsigned int crc = CRC24_INIT;
  1371.     int i;
  1372.  
  1373.     while (len--) {
  1374. /*@-boundsread@*/
  1375.     crc ^= (*octets++) << 16;
  1376. /*@=boundsread@*/
  1377.     for (i = 0; i < 8; i++) {
  1378.         crc <<= 1;
  1379.         if (crc & 0x1000000)
  1380.         crc ^= CRC24_POLY;
  1381.     }
  1382.     }
  1383.     return crc & 0xffffff;
  1384. }
  1385.  
  1386. /** \ingroup rpmio
  1387.  * Duplicate a digest context.
  1388.  * @param octx        existing digest context
  1389.  * @return        duplicated digest context
  1390.  */
  1391. /*@only@*/
  1392. DIGEST_CTX rpmDigestDup(DIGEST_CTX octx)
  1393.     /*@*/;
  1394.  
  1395. /** \ingroup rpmio
  1396.  * Initialize digest.
  1397.  * Set bit count to 0 and buffer to mysterious initialization constants.
  1398.  * @param hashalgo    type of digest
  1399.  * @param flags        bit(s) to control digest operation
  1400.  * @return        digest context
  1401.  */
  1402. /*@only@*/ /*@null@*/
  1403. DIGEST_CTX rpmDigestInit(pgpHashAlgo hashalgo, rpmDigestFlags flags)
  1404.     /*@*/;
  1405.  
  1406. /** \ingroup rpmio
  1407.  * Update context with next plain text buffer.
  1408.  * @param ctx        digest context
  1409.  * @param data        next data buffer
  1410.  * @param len        no. bytes of data
  1411.  * @return        0 on success
  1412.  */
  1413. int rpmDigestUpdate(/*@null@*/ DIGEST_CTX ctx, const void * data, size_t len)
  1414.     /*@modifies ctx @*/;
  1415.  
  1416. /** \ingroup rpmio
  1417.  * Return digest and destroy context.
  1418.  * Final wrapup - pad to 64-byte boundary with the bit pattern 
  1419.  * 1 0* (64-bit count of bits processed, MSB-first)
  1420.  *
  1421.  * @param ctx        digest context
  1422.  * @retval datap    address of returned digest
  1423.  * @retval lenp        address of digest length
  1424.  * @param asAscii    return digest as ascii string?
  1425.  * @return        0 on success
  1426.  */
  1427. int rpmDigestFinal(/*@only@*/ /*@null@*/ DIGEST_CTX ctx,
  1428.     /*@null@*/ /*@out@*/ void ** datap,
  1429.     /*@null@*/ /*@out@*/ size_t * lenp, int asAscii)
  1430.         /*@modifies *datap, *lenp @*/;
  1431.  
  1432. #ifdef __cplusplus
  1433. }
  1434. #endif
  1435. /*@=fcnuse@*/
  1436.  
  1437. #endif    /* H_RPMPGP */
  1438.